﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

namespace WordTemplate2.Pannel
{
    public partial class AddAppendix : Form
    {
        int limit_domainUpDown = 0;

        public int GetLastTemplateIndex()
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;

            bool flag = Globals.ThisDocument.CheckBookmarkExist("mbookmark5");
            if (!flag)
                return 0;
            else
            {
                Globals.ThisDocument.MoveBookmarkStart("mbookmark5");
                Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark5");
                string mark = "";
                do
                {
                    string str = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListString;
                    if(str.Contains("附"))
                    {
                        mark = str;
                    }
                    Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                }
                while (Globals.ThisDocument.Application.Selection.Range.End <= r.End);

                if (mark != "")
                {
                    char unicode = mark[mark.Length - 1];
                    mark = "A";
                    char unicodeA = mark[mark.Length - 1];
                    int length = unicode - unicodeA;
                    return length + 1;
                }
            }
            return 0;
        }

        public void RemoveLastBreak()
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;
            int page = (int)Globals.ThisDocument.Application.Selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);
            bool flag = false;
            while (!flag)
            {
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                int page1 = (int)Globals.ThisDocument.Application.Selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);
                if (page1 < page)
                {
                    Globals.ThisDocument.Application.Selection.TypeParagraph();
                    flag = true;
                }
            }
        }

        public int GetPositionbyliststring(string target_str)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;

            Globals.ThisDocument.MoveBookmarkStart("mbookmark5");
            Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark5");
            do
            {
                string str = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListString;
                if (str.Contains("附") && str.Contains(target_str))
                {
                    return Globals.ThisDocument.Application.Selection.Range.End;
                }
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
            }
            while (Globals.ThisDocument.Application.Selection.Range.End <= r.End);

            return 0;

        }

        public AddAppendix()
        {
            InitializeComponent();
            limit_domainUpDown = GetLastTemplateIndex();
            domainUpDown1.SelectedIndex = limit_domainUpDown;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Word.Range range = Globals.ThisDocument.GetBookmarkRange("mbookmark5");
            this.Visible = false;
            this.Close();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;

            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入附录的标题，再按确定按钮！");
                return;
            }
            bool flag = Globals.ThisDocument.CheckBookmarkExist("mbookmark5");
            if (!flag)
            {
                Globals.ThisDocument.MoveBookmarkEnd("mbookmark4");

                if (Globals.ThisDocument.CheckBookmarkExist("mbookmark6") || Globals.ThisDocument.CheckBookmarkExist("mbookmark7"))
                {
                    Globals.ThisDocument.MoveBookmarkEnd("mbookmark4");
                    object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                    Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                }
                else
                {
                    Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
                }
                
                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                object range = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark5", ref range);
                Globals.ThisDocument.ReplaceBookmarkContentWithFileBookmark("mbookmark5", "标准模板.doc");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_index", textBox1.Text);
                if (radioButton1.Checked)
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_type", "资料性附录");
                }
                else
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_type", "规范性附录");
                }


        
                Globals.ThisDocument.RemoveBookmark("mbookmark5_index");
                Globals.ThisDocument.RemoveBookmark("mbookmark5_type");

            }
            else
            {
                Globals.ThisDocument.MoveBookmarkEnd("mbookmark5");

                if (domainUpDown1.SelectedIndex == limit_domainUpDown)
                {
                    Globals.ThisDocument.MoveBookmarkEnd("mbookmark5");
                    Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref move);
                    Globals.ThisDocument.Application.Selection.TypeText(" ");
                    Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);

                    if (radioButton3.Checked)
                    {
                        Globals.ThisDocument.Application.Selection.TypeParagraph();
                        object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                        Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    }
                    else if (radioButton4.Checked)
                    {
                        Globals.ThisDocument.Application.Selection.TypeParagraph();
                    }
                }
                else
                {
                    int pos = GetPositionbyliststring(domainUpDown1.Text);
                    Globals.ThisDocument.Application.Selection.Range.Start = pos;
                    Globals.ThisDocument.Application.Selection.Range.End = pos;
                    
                    if (radioButton3.Checked)
                    {
                        Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref move);
                        object pagebreak = Word.WdBreakType.wdSectionBreakNextPage;
                        Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);
                    }
                    else if (radioButton4.Checked)
                    {
                        Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref move);
                        Globals.ThisDocument.Application.Selection.TypeText(" ");
                        Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref number, ref extend);
                        Globals.ThisDocument.Application.Selection.TypeParagraph();
                    }
                }


                Globals.ThisDocument.SelectionParseFromFileBookmark("标准模板.doc", "mbookmark5", true);
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_index", textBox1.Text);


                if (radioButton1.Checked)
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_type", "资料性附录");
                }
                else
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5_type", "规范性附录");
                }
                Globals.ThisDocument.RemoveBookmark("mbookmark5_index");
                Globals.ThisDocument.RemoveBookmark("mbookmark5_type");
                Globals.ThisDocument.RemoveBookmark("temp");
              
            }



            this.Visible = false;
            this.Close();
        }

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            if (domainUpDown1.SelectedIndex > limit_domainUpDown)
            {
                domainUpDown1.SelectedIndex = limit_domainUpDown;
            }

            if(domainUpDown1.SelectedIndex > 0)
                radioButton4.Enabled = true;
            else
                radioButton4.Enabled = false;

            if(domainUpDown1.SelectedIndex < limit_domainUpDown)
                radioButton5.Enabled = true;
            else
                radioButton5.Enabled = false;
        }


    }
}
